package com.Txunda.fxw.ui.base;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.BaseActivity;
import com.Txunda.fxw.base.utils.util.PreferencesUtils;
import com.Txunda.fxw.config.Settings;
import com.Txunda.fxw.ui.activity.login.LoginAty;
import com.Txunda.fxw.ui.activity.other.SettingAty;
import com.Txunda.fxw.ui.http.FeedBack;
import com.Txunda.fxw.ui.listener.GlideImageLoader;
import com.Txunda.fxw.ui.listener.ShareCallBack;
import com.Txunda.fxw.ui.toolkit.config.Config;
import com.Txunda.fxw.ui.toolkit.share.ShareUtils;
import com.Txunda.fxw.view.dialog.AlertDialog;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.view.CropImageView;

import java.util.List;

import cn.sharesdk.framework.Platform;


public abstract class BaseAty extends BaseActivity implements ShareCallBack {

    private ShareUtils shareUtils;
    private Bitmap bitmap;

    private DrawerLayout drawerLayout;
    private FeedBack feedBack;

    protected String type = "";
    protected boolean likes = false;
    protected String ids = "";

    private String likesId;

    private ImageView likesImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
        shareUtils = new ShareUtils(bitmap, this);
        feedBack = new FeedBack();
    }

    public void showRightTip(String tips) {
        showTips(R.drawable.icon_right_tip, tips);
    }

    public void showErrorTip(String tips) {
        showTips(R.drawable.error, tips);
    }

    //返回按钮监听事件
    public void finish(View v) {
        this.finish();
    }

    // 图片选择框架初始化
    protected void initImagePicker() {
        int size = Settings.displayWidth / 4;
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        imagePicker.setShowCamera(true);// 显示相机
        imagePicker.setMultiMode(false);// 单选
        imagePicker.setCrop(true);// 裁剪
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);
        Integer width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
        Integer height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(width);
        imagePicker.setFocusHeight(height);
        imagePicker.setOutPutX(size);
        imagePicker.setOutPutY(size);
    }

    /**
     * 判断是否安装qq客户端
     */
    public boolean isQQClientAvailable() {
        final PackageManager packageManager = this.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否已安装微信客户端
     */
    public boolean isWeixinAvilible() {
        final PackageManager packageManager = this.getPackageManager();
        // 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        // 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    // 设置
    public void settings(View view) {
        startActivity(SettingAty.class, null);
        closeSide();
    }

    // 打电话
    public void callphone(View view) {
        new AlertDialog(this).builder().setTitle("提示").setMsg("tel: 022-60688296")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel: 022-60688296"));
                        startActivity(intent);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        }).show();
        closeSide();
    }

    // 新浪微博分享
    public void singe(View view) {
        showTips(R.drawable.icon_right_tip, "暂未开放");
    }

    // 微信分享
    public void wechar(View view) {
        if (!isWeixinAvilible()) {
            showToast("请下载并安装微信客户端");
            return;
        }
        shareUtils.onWechatShare();
        closeSide();
    }

    // 朋友圈
    public void friend(View view) {
        if (!isWeixinAvilible()) {
            showToast("请下载并安装微信客户端");
            return;
        }
        shareUtils.onWechatFriends();
        closeSide();
    }

    // QQ分享
    public void qq(View view) {
        if (!isQQClientAvailable()) {
            showToast("请下载并安装QQ客户端");
            return;
        }
        shareUtils.onQQFriends();
        closeSide();
    }

//    // 评价
//    public void evaluate(View view) {
//        Bundle bundle = new Bundle();
//        bundle.putString("type", type);
//        startActivity(EvaluateAty.class, bundle);
//        closeSide();
//    }

    // 进入评价页面
    public void toEvaluate() {
        if (!Config.isLogin()) {
            startActivity(LoginAty.class, null);
            return;
        }
        showProgressDialog();
        String m_id = application.getUserInfo().get("m_id");
        feedBack.comment(m_id, this);
    }


    // 关闭侧边栏
    private void closeSide() {
        if (drawerLayout != null && drawerLayout.isDrawerOpen(Gravity.END)) {
            drawerLayout.closeDrawers();
        }
    }

    // 设置侧边栏布局
    public void setDrawerLayout(DrawerLayout drawerLayout) {
        this.drawerLayout = drawerLayout;
    }

    // TODO ==========分享回调
    @Override
    public void onSuccess(String plantName) {
        showTips(R.drawable.icon_right_tip, plantName + "分享成功");
    }

    @Override
    public void onError(String plantName, Platform platform, int i, Throwable throwable) {
        showTips(R.drawable.icon_right_tip, plantName + "分享失败");
    }

    @Override
    public void onCancle(String plantName) {
        showTips(R.drawable.icon_right_tip, plantName + "取消成功");
    }

    public void onLikes(View view) {
        if (likes) {
            ids = ids.replaceAll(type + likesId + ",", "");
            likesImage.setImageResource(R.drawable.icon_collect3);
            likes = false;
        } else {
            ids = ids + type + likesId + ",";
            likesImage.setImageResource(R.drawable.icon_yet_collect);
            likes = true;
        }
        PreferencesUtils.putString(this, "likes", ids);
    }

    public void getLikesId(ImageView sideBarLikeIv, String classId) {
        likesImage = sideBarLikeIv;
        likesId = classId;
        ids = PreferencesUtils.getString(this, "likes");
        likes = !TextUtils.isEmpty(ids) && ids.contains(type + classId);
        if (likes) {
            sideBarLikeIv.setImageResource(R.drawable.icon_yet_collect);
        } else {
            sideBarLikeIv.setImageResource(R.drawable.icon_collect3);
        }
    }


    // 销毁时清掉bitmap
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }
}
